package com.sanalkitaplik.hibernateutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import org.hibernate.Query;
import org.hibernate.Session;

public class StaticInfo {
	
	private static Connection connection = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;
	private static ArrayList <String> list = null;
	
	/* Sistemde kayıtlı tüm yazar isimlerini döndürür.*/
	public static ArrayList<String> getAuthorName() throws SQLException{
		connection = Connect.getConnection();
		stmt = connection.createStatement();
		rs = stmt.executeQuery("select AUTHOR_NAME from SNLKTPLK_AUTHORS");
		
		list = new ArrayList<String>(rs.getRow());
		while(rs.next()){
			list.add(rs.getString("AUTHOR_NAME"));
		}
		
		return list;
	}
	
	/*Sisteme yeni bir yazar ekler. Ekleyebilirse TRUE, ekleyemezse FALSE döndürür.*/
	public static boolean addAuthorName(String author_name) throws SQLException{
		boolean flag = true;
		Session session = null;
		try{
			session = SessionFactoryBuilder.getSessionFactory().openSession();
			Query query = session.createSQLQuery("select AUTHOR_ID from SNLKTPLK_AUTHORS where AUTHOR_NAME='"+author_name+"'");
			if(query.list().isEmpty()){
				connection = Connect.getConnection();
				stmt = connection.createStatement();
				stmt.executeUpdate("insert into SNLKTPLK_AUTHORS values (default,'"+author_name+"')");
			}
			else
				flag = false;
		}
		catch(Exception ex){
			flag = false;
			System.out.println("An exception occured while adding an author to the system : "+ex.getMessage());
		}
		finally{
			session.flush();
			session.close();
			connection.close();
			stmt.close();
		}
		return flag;
	}
	
	/*Sistemde kayıtlı kullanıcı türlerini döndürür.*/
	public static ArrayList<String> getTypeName() throws SQLException{
		connection = Connect.getConnection();
		stmt = connection.createStatement();
		rs = stmt.executeQuery("select TYPE_NAME from SNLKTPLK_TYPE_INFO");
		
		list = new ArrayList<String>(rs.getRow());
		while(rs.next()){
			list.add(rs.getString("TYPE_NAME"));
		}
		
		return list;
	}
	
	/*Sistemde kayıtlı kaynak türlerini döndürür.*/
	public static ArrayList<String> getGenre() throws SQLException{
		connection = Connect.getConnection();
		stmt = connection.createStatement();
		rs = stmt.executeQuery("select GENRE_NAME from SNLKTPLK_GENRES");
		
		list = new ArrayList<String>(rs.getRow());
		while(rs.next()){
			list.add(rs.getString("GENRE_NAME"));
		}
		
		return list;
	}
	
	/*Sistemde kayıtlı tüm bölüm isimlerini döndürür.*/
	public static ArrayList<String> getDepartmentInfo() throws SQLException{
		connection = Connect.getConnection();
		stmt = connection.createStatement();
		rs = stmt.executeQuery("select DEPARTMENT_NAME from SNLKTPLK_DEPARTMENT_INFO");
		
		list = new ArrayList<String>(rs.getRow());
		while(rs.next()){
			list.add(rs.getString("DEPARTMENT_NAME"));
		}
		
		return list;
	}
}
